"""两种方法1。获取年费、2.获取专利信息"""
import requests
import json
import time
import pymysql
import redis
import random

conn = pymysql.connect(host="121.4.69.146",
                       user='root',
                       port=3306,
                       password='xhhg@2020',
                       database='big_data',
                       cursorclass=pymysql.cursors.DictCursor)
redis_con = redis.Redis(
    host='121.4.69.146', password='xhhg123456', port=6389, db=2, decode_responses=True)


def get_patent_pay(session, authorization, application_number):
    status_post_url = r'https://cpquery.cponline.cnipa.gov.cn/api/view/gn/fyxx'
    headers = {'Accept': 'application/json, text/plain, */*', 'Accept-Encoding': 'gzip, deflate, br',
               'Accept-Language': 'zh-CN,zh;q=0.9',
               'Authorization': """%s""" % authorization,
               'Cache-Control': 'no-cache',
               'Connection': 'keep-alive',
               'Content-Length': '30',
               'Content-Type': 'application/json;charset=UTF-8',
               'Host': 'cpquery.cponline.cnipa.gov.cn',
               'Origin': 'https://cpquery.cponline.cnipa.gov.cn',
               'Pragma': 'no-cache',
               'Referer': 'https://cpquery.cponline.cnipa.gov.cn/detail/index?zhuanlisqh=%s&anjianbh' % application_number,
               'Sec-Fetch-Dest': 'empty',
               'Sec-Fetch-Mode': 'cors',
               'Sec-Fetch-Site': 'same-origin',
               'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36',
               'sec-ch-ua': '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"',
               'sec-ch-ua-mobile': '?0',
               'sec-ch-ua-platform': '"Windows"',
               'userType': 'USER_RZ_DAILIJIG'}
    data = {"zhuanlisqh": application_number}
    patent_pay_res = session.post(
        status_post_url, headers=headers, data=json.dumps(data))
    patent_pay_data = patent_pay_res.json().get('data')

    # 应缴费信息
    yinjiaofei_data = patent_pay_data.get('yingjiaofei')
    yinjiaofei_list = yinjiaofei_data.get('svYingjfList')
    for rows in yinjiaofei_list:
        yinjiao_sql = f'''insert into annual_fee_information_0321 (application_number, money_type, money, close_date, pay_status, created_at)
      values('{application_number}', '{rows.get('yingjiaoffyzlmc')}', '{rows.get('yingjiaoje')}', '{rows.get('jiaofeijzr')}', '{rows.get('yingjiaoffyzt')}', NOW())'''
        try:
            cur = conn.cursor()
            cur.execute(yinjiao_sql)
            conn.commit()
        except:
            print(yinjiao_sql)
    print('@@application_number:%s--应缴费信息入库完成@@' % application_number)
    # 已缴费信息
    yijiaofei_data = patent_pay_data.get('yijiaofei')
    yijiaofei_list = yijiaofei_data.get('svYijfList')
    for rows in yijiaofei_list:
        yijiao_sql = f'''insert into by_payment_information_0321(cost_category, payment_amount, pay_date, name_payer, note_the_code, ticket_number, create_time, application_number)
            values('{rows.get('yijiaofjfzlmc')}', '{rows.get('yijiaofjfje')}', '{rows.get('yijiaofjfrq')}', '{rows.get('yijiaofjfrxm')}', '{rows.get('yijiaofpjdm')}', '{rows.get('yijiaofpjhm')}', NOW(), '{application_number}')'''
        try:
            cur = conn.cursor()
            cur.execute(yijiao_sql)
            conn.commit()
        except:
            print(yijiao_sql)
    print('##application_number:%s--已缴费信息入库完成##' % application_number)
    # 滞纳费信息
    zhinafei_data = patent_pay_data.get('zhinajin')
    zhinafei_list = zhinafei_data.get('svZnjList')
    for rows in zhinafei_list:
        date_str = rows.get('zhinajjfsj')
        date_list = date_str.split('到')
        date_list = [item.replace(
            '年', '-').replace('月', '-').replace('日', '') for item in date_list]
        start_date = date_list[0]
        end_date = date_list[1]
        zhinajin_sql = f'''insert into overdue_fine_0321(application_number, payment_start_time, payment_end_time, current_annual_fee, late_fee, total, create_time, updata_time) values('{application_number}','{start_date}','{end_date}','{rows.get('zhinajdqnfje')}','{rows.get('zhinajyjznje')}','{rows.get('zhinajzj')}',NOW(),NOW())'''
        try:
            cur = conn.cursor()
            cur.execute(zhinajin_sql)
            conn.commit()
        except:
            print(zhinajin_sql)
    print('**application_number:%s--已缴费信息入库完成**' % application_number)
    if yinjiaofei_list == [] and yijiaofei_list == [] and zhinafei_list == []:
        redis_con.sadd("外部接口接口查询", application_number)


def run(port, authorization):
    session = requests.session()
    while redis_con.scard(port):
        sqh = redis_con.spop(port).replace("ZL", '')
        get_patent_pay(session, authorization, sqh)
        time.sleep(random.randint(2, 3))


def prease_data(session, url, headers, i, name, is_flag):
    num = 0
    data = {
        "page": i,
        "searchType": "sqr",
        "shenqingrxm": name,
        "size": 100,
        "sortDataName": "",
        "sortType": "",
        "zhuanlilx": "",
    }
    patent_pay_res = session.post(url, headers=headers, data=json.dumps(data))
    print(patent_pay_res)
    main_data = patent_pay_res.json()['data']['records']
    if main_data:
        for row in main_data:
            num += 1
            item = {}
            item['sqh'] = row['zhuanlisqh']
            item['zhuanlimc'] = row['zhuanlimc']  # 专利名
            item['state'] = row['anjianywzt']
            item['low_state'] = row['falvzt'] if row['falvzt'] else ""  # 法律状态
            item['sq_data'] = row['gongkaiggr'] if row['gongkaiggr'] else ""  # 授权日
            item['shenqingr'] = row['shenqingr']  # 申请日
            item['shenqingrxm'] = row['shenqingrxm']  # 申请人

            zhuanlilx = row['zhuanlilx']
            if zhuanlilx == "1":
                item['type'] = "发明专利"
            elif zhuanlilx == "2":
                item['type'] = "实用新型"
            else:
                item['type'] = '外观设计'
            item['shouquanggh'] = row['shouquanggh'] if row['shouquanggh'] else ""  # 授权公告号
            item['fm_class'] = row['zhufenlh']  # 主分类号
            # print(item)
            insert_db(item)         # r入库
        if i % 2 == 0:
            conn.commit()
        conn.commit()
    else:
        print("=" * 50 + f"{name}数据已经抓完,抓取{num}条数剧，开始抓取下一个机构数据" + "=" * 50)
        is_flag = False
    return is_flag


def insert_db(item):
    print(item)
    insert_sql = f"INSERT INTO all_hospital_patent_date_TOTAL(sqh, `name`, state, sqr, shouquanr,low_state, patent_user, type, sqggh, fm_class) VALUES('{item['sqh']}', '{item['zhuanlimc']}', " \
                 f"'{item['state']}', '{item['shenqingr']}', '{item['sq_data']}', '{item['low_state']}', '{item['shenqingrxm']}', '{item['type']}', '{item['shouquanggh']}', '{item['fm_class']}')"
    try:
        cur = conn.cursor()
        cur.execute(insert_sql)
    except:
        print("insert_sql: ", insert_sql)


def get_li():
    """aaa.txt文件记录专利权人名称"""
    hospital_li = []
    with open('records_sqh.txt', 'r', encoding='utf-8') as fr:
        for i in fr:
            hospital_li.append(i.strip())
    return hospital_li


def main():
    hospital_li = get_li()      # 可以批量获取多家机构 的专利信息
    session = requests.session()
    url = r"https://cpquery.cponline.cnipa.gov.cn/api/search/undomestic/publicSearch"      # url随着登陆账号改变而改变，自然人不会变化，XH账号跟XHHG账号的url是相同的（非公众查询）
    headers = {
        'Accept': 'application/json, text/plain, */*',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Authorization': 'Bearer eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJuZXVzb2Z0IiwiaWF0IjoxNjgyNDg5MDk3LCJleHAiOjE2ODI1MTc4OTcsImFjY2Vzc190b2tlbiI6ImZlNGJiOTllLTI5NDYtNDY4My1iOTZlLWI2NGUxZmExMTY1MSIsInJlZnJlc2hfdG9rZW4iOiIyZWU0OGQzYS1jYzI2LTQxOWEtYTk1My03MTM1NDA4OWQyYWYiLCJ1c2VySWQiOiIxNTYwMTU1NTMyMSIsIkV4cGlyZXNfaW4iOjI4ODAwfQ.JE-5PbEKqps4_JA88Ak0pb-VsQ5W2HLkBXLbOEwp1qdpg6DIVOBlPWV0Pd8vjfjXT11XUg0k1NaFsiRxpYJyFEFt6EMQyU7QuKQmwmFdAJniG--ACXyI1LwAebobV_ARa1L8qABc3-pQPHjjyQnwePje3zzGRtgLMKq-MeJtZPE',
        'Connection': 'keep-alive',
        'Content-Length': '125',
        'Content-Type': 'application/json;charset=UTF-8',
        'Cookie': '_gscu_1382584180=69285472xdmfc499; _trs_uv=ld1d1zch_4693_f4w3; dX1xbeyMT58WO=5ht7IWrUfLNNB1bsyXg3hCcIHuwvIbDXWpqKbggLWkZ9VwNPvCErPwxkJpAEErTaT486BpeXZ_TpcV4Db_QKoCa',
        'Host': 'cpquery.cponline.cnipa.gov.cn',
        'Origin': 'https://cpquery.cponline.cnipa.gov.cn',
        'Referer': 'https://cpquery.cponline.cnipa.gov.cn/chinesepatent/index',
        'sec-ch-ua': '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'same-origin',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36',
        'userType': 'USER_RZ_DAILIJIG',
    }
    for name in hospital_li:
        print(name)
        is_flag = True
        i = 1
        while is_flag:
            is_flag = prease_data(session, url, headers, i, name, is_flag)     # 请求接口数据并处理所需信息
            time.sleep(random.uniform(2, 4))
            i += 1


if __name__ == '__main__':
    port = ''
    # authorization = 'Bearer eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJuZXVzb2Z0IiwiaWF0IjoxNjgyMjI2NDYzLCJleHAiOjE2ODIyNTUyNjMsImFjY2Vzc190b2tlbiI6ImQxNjRkYjkxLTVhNzEtNGJiNC04NjYyLTc0OTk0ZjgyMDJmMyIsInJlZnJlc2hfdG9rZW4iOiI0NTU4NDQwMS02YWVlLTRkZjYtOTU2Mi03ZDZiOGIwMGFjYjciLCJ1c2VySWQiOiIxNTYwMDY1NDY2MCIsIkV4cGlyZXNfaW4iOjI4ODAwfQ.NpVSCDCtXMbVw1w0M2_XGJOn4tduGUaRWO5CXYgwEsOH2ShotZ4CCs9pHAncfyNQmW3CyRmDLEJqz015Pw_VRExOdU2YlOyz0VzsspNyPiqJdgoie3xJjp_rEWR0VIYUwL0yih3N1Fd9VaLwHEusmsL3oG_dE1gP6Y1Db176ARk'
    # apply_num = '2021201001810'
    # get_patent_pay(session, authorization, apply_num)
    # run(port, authorization)    # 专利费用信息抓取
    main()  # 专利状态抓取
    conn.close()


#     d0ULoAlqElWmyvJ3uJoqi5NQDjsR35KGNL0TTKfaRIzRHWIsneEiEt4QDhjOUEApTFCmTbIkqqp7Au7xxMTKmbWE.rPznDJY
